package com.xiao.mall.foundation.utils;

import org.springframework.util.StringUtils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class DateUtil {
    private static final String ALG_DATE_FORMAT = "yyyyMMdd";


    public static Instant mergeDateAndTime(LocalDate date, Instant time) {
        return LocalDateTime.ofInstant(time, ZoneId.systemDefault())
                .withYear(date.getYear())
                .withMonth(date.getMonthValue())
                .withDayOfMonth(date.getDayOfMonth())
                .atZone(ZoneId.systemDefault())
                .toInstant();
    }

    public static LocalDate algDateToLocalDate(String algDateStr) {
        return LocalDate.parse(algDateStr.substring(0,8), DateTimeFormatter.ofPattern(ALG_DATE_FORMAT));

    }

    public static LocalDate algDateToWeek(String algDateStr) {
        LocalDate localDate = algDateToLocalDate(algDateStr);
        return localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
    }

    public static LocalDate algDateToMonth(String algDateStr) {
        LocalDate localDate = algDateToLocalDate(algDateStr);
        return localDate.with(TemporalAdjusters.firstDayOfMonth());
    }

    public static Boolean IsInterSection(Instant startDateOne, Instant endDateOne, Instant startDateTwo, Instant endDateTwo) {
        Instant maxStartDate = startDateOne;
        if (maxStartDate.isBefore(startDateTwo)) {
            maxStartDate = startDateTwo;
        }

        Instant minEndDate = endDateOne;
        if (endDateTwo.isBefore(minEndDate)) {
            minEndDate = endDateTwo;
        }

        if (maxStartDate.isBefore(minEndDate) || (maxStartDate.compareTo(minEndDate) == 0)) {
            return true;
        } else {
            return false;
        }

    }


    public static int calIntersectionTimeSeconds(LocalDateTime startDateOne, LocalDateTime endDateOne, LocalDateTime startDateTwo, LocalDateTime endDateTwo) {
        LocalDateTime maxStartDate = startDateOne;
        if (maxStartDate.isBefore(startDateTwo)) {
            maxStartDate = startDateTwo;
        }

        LocalDateTime minEndDate = endDateOne;
        if (endDateTwo.isBefore(minEndDate)) {
            minEndDate = endDateTwo;
        }

        if (maxStartDate.isBefore(minEndDate) || (maxStartDate.compareTo(minEndDate) == 0)) {
            Duration duration = Duration.between(maxStartDate, minEndDate);
            //相差毫秒数
            int seconds = (int) (duration.toMillis() / 1000);
            return seconds;
        }
        return 0;
    }

    public static LocalDateTime formatStringForLocalDateTime(String dateStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime startDateTime = LocalDateTime.parse(dateStr, dateTimeFormatter);
        return startDateTime;
    }

    public static LocalDate parseLocalDateForString(String dateStr, String pattern) {

        pattern = StringUtils.hasText(pattern) ? pattern : "yyyy-MM-dd";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        if (StringUtils.hasText(dateStr)) {
            return LocalDate.parse(dateStr, dateTimeFormatter);
        } else {
            return null;
        }
    }

    /**
     * 日期转换为LocalDateTime
     * @param dateStr
     * @param pattern
     * @return
     */
    public static LocalDateTime parseLocalDateTimeForString(String dateStr, String pattern) {

        pattern = StringUtils.hasText(pattern) ? pattern : "yyyy-MM-dd HH:mm:ss";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        if (StringUtils.hasText(dateStr)) {
           return LocalDateTime.parse(dateStr, dateTimeFormatter);
        } else {
            return null;
        }
    }

    public static Instant formatStringForInstant(String dateStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Instant startDateTime = Instant.parse(dateStr);
        return startDateTime;
    }

    public static String parseStringFromInstant(Instant instant) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        String dateStr = dateTimeFormatter.format(localDateTime);
        return dateStr;
    }

    public static String formatStringForLocalDate(LocalDate localDate, String pattern) {
        pattern = StringUtils.hasText(pattern) ? pattern : "yyyy-MM-dd";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        if (localDate == null) {
            return null;
        }
        return dateTimeFormatter.format(localDate);
    }

    public static String formatStringForLocalDateTime(LocalDateTime localDateTime, String pattern) {
        pattern = StringUtils.hasText(pattern) ? pattern : "yyyy-MM-dd HH:mm:ss";
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        if (localDateTime == null) {
            return null;
        }
        return dateTimeFormatter.format(localDateTime);
    }

    public static LocalDateTime formatStringForInstantyyyyMMdd(String dateStr) {
        return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));

    }

    public static String timeToStrDate(LocalDateTime time){
        if (time == null){
            return "";
        }
        return time.format(DateTimeFormatter.ofPattern(ALG_DATE_FORMAT));
    }

    public static Long dateDiff(LocalDateTime dt1,LocalDateTime dt2){
        //获取第一个时间点的时间戳对应的秒数
        long t1 = dt1.toEpochSecond(ZoneOffset.ofHours(0));
        //获取第一个时间点在是1970年1月1日后的第几天
        long day1 = t1 /(60*60*24);
        //获取第二个时间点的时间戳对应的秒数
        long t2 = dt2.toEpochSecond(ZoneOffset.ofHours(0));
        //获取第二个时间点在是1970年1月1日后的第几天
        long day2 = t2/(60*60*24);
        //返回两个时间点的天数差
        return (Long)(day2 - day1);
    }

    /**
     * 时间是否在区间内
     * @param nowTime
     * @param startTimeStr
     * @param endTimeStr
     */
    public static boolean isEffectTime(LocalDateTime nowTime, String startTimeStr, String endTimeStr) {

        boolean isEffectTime = false;
        String formatStringForLocalDate = DateUtil.formatStringForLocalDate(nowTime.toLocalDate(), "yyyy-MM-dd");
        LocalDateTime startTime = parseLocalDateTimeForString(formatStringForLocalDate + " " + startTimeStr, "yyyy-MM-dd HH:mm:ss");
        LocalDateTime endTime = parseLocalDateTimeForString(formatStringForLocalDate + " " + endTimeStr, "yyyy-MM-dd HH:mm:ss");
        //在时间区间内
        if ((nowTime.isAfter(startTime) || nowTime.isEqual(startTime))
                && (nowTime.isBefore(endTime) || nowTime.isEqual(endTime))) {
            isEffectTime = true;
        }

        return isEffectTime;
    }

    /**
     * 日期字符串转换
     *
     * @param str str
     * @return LocalDateTime
     */
    public static LocalDateTime convertToLocalDateTime(String str) {
        if (!StringUtils.hasText(str)) {
            return null;
        }
        return LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}
